package com.taiyi.renaissancemall.Utils

import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.widget.ImageView
import androidx.core.view.isVisible
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.snackbar.Snackbar
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.base.MyApp

fun View.snack(msg: String) {
    if (msg.isNotEmpty()) {
        Snackbar.make(this, msg, Snackbar.LENGTH_SHORT).show()
    }
}

fun ImageView.load(url: String) {
    var transform = RequestOptions().skipMemoryCache(true)
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE).placeholder(R.mipmap.ic_launcher_foreground)
    if (url.isNotEmpty()) {
        Glide.with(MyApp.getInstance()).load(url).apply(transform).into(this)
    }
}

fun ImageView.loadCircle(url: String) {
    if (url.isNotEmpty()) {
        var transform = RequestOptions().skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .placeholder(R.mipmap.ic_launcher_foreground)

        Glide.with(MyApp.getInstance()).load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop())).apply(transform)
            .into(this)
    }
}

fun ImageView.loadRound(url: String) {//加载圆角图片
    if (url.isNotEmpty()) {
        var requestOptions = RequestOptions().skipMemoryCache(true)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .placeholder(R.mipmap.ic_launcher_foreground)
        var roundedCorners = RoundedCorners(50)
        requestOptions.transform(roundedCorners)
        Glide.with(MyApp.getInstance()).load(url)
            .apply(requestOptions)
            .into(this)
    }
}


fun View.isVisibility() {
    visibility = if (isVisible) {
        GONE
    } else {
        VISIBLE
    }


}